<?php
/*
*     蝴蝶数据云采集服务 butterfly_data
*	  @ARPG TEAM. 2013
*	  http://butterfly.arpggame.com
*/

if(!defined('IN_DISCUZ') || !defined('IN_ADMINCP')) {
	exit('Access Denied');
}


define('CHARSET',$_G['charset']);
require_once('class/common.php');

$type = addslashes($_GET['type']);


if ($type == "save_config") {
	
	$name 			= trim(addslashes($_POST['name']));
	$form_site 		= trim(addslashes($_POST['form_site']));
	$to_fid 		= trim(intval($_POST['to_fid']));
	$is_rewords		= trim(addslashes($_POST['is_rewords']));
	$replace_word 	= trim(addslashes($_POST['replace_word']));
	$before_title 	= trim(addslashes($_POST['before_title']));
	$after_title 	= trim(addslashes($_POST['after_title']));
	$before_content = trim(addslashes($_POST['before_content']));
	$after_content 	= trim(addslashes($_POST['after_content']));
	$rand_content 	= trim(addslashes($_POST['rand_content']));
	$uid_min 		= trim(intval($_POST['uid_min']));
	$uid_max 		= trim(intval($_POST['uid_max']));
	$browsernum_min = trim(intval($_POST['browsernum_min']));
	$browsernum_max = trim(intval($_POST['browsernum_max']));
	$is_downpic 	= trim(intval($_POST['is_downpic']));

	if ($name == "") {
		cpmsg_error('请输入采集器名称');
	}
	if ($uid_min == 0 || $uid_max == 0) {
		cpmsg_error('请输入UID范围, 如只使用一个UID发布请设置为 1~1即可');
	}
	if ($browsernum_min == 0 || $browsernum_max == 0) {
		cpmsg_error('请输入主题浏览量区间');
	}

	$is_fid = DB::fetch_first("SELECT * FROM ".DB::table('forum_forum')." WHERE  $where fid = '$to_fid'");
	if ($is_fid[type] != "forum") {
		cpmsg_error('请选择正确的版块，无法发布至分区！');
	}

	$str = array(
		'name' => $name,
		'form_site' => $form_site,
		'to_fid' => $to_fid,
		'is_rewords' => $is_rewords,
		'replace_word' => $replace_word,
		'before_title' => $before_title,
		'after_title' => $after_title,
		'before_content' => $before_content,
		'after_content' => $after_content,
		'rand_content' => $rand_content,
		'uid_min' => $uid_min,
		'uid_max' => $uid_max,
		'browsernum_min' => $browsernum_min,
		'browsernum_max' => $browsernum_max,
		'is_downpic' => $is_downpic
	);

	$result = DB::query('INSERT INTO butterfly_config SET '.bfsql_fir($str));
	if($result) {
		cpmsg('添加成功！如需开始任务请前往一键采集', 'action=plugins&operation=config&do=31&identifier=arpggame_butterfly_data&pmod=butterfly_config', 'succeed');
	} else{
		cpmsg_error('添加失败，请重试！');
	}
}else{

	$forum_list = $forum_cate = $forum_type_list = $forum_arr =  array();
	$query = DB::query("select f.fid,f.name,f.fup,f.type,ff.threadtypes from ".DB::table('forum_forum')." f left join ".DB::table('forum_forumfield')." ff using(fid) where f.status = '1'");
	while($rt = DB::fetch($query)) {
		if ($rt['type'] == 'group') {
			$forum_list[$rt['fid']]['name'] = $rt['name'];
		} elseif ($rt['type'] == 'forum') {
			$forum_arr[$rt['fid']] = $rt['name'];
			$forum_cate[$rt['fid']] = $rt['fup'];
			$forum_list[$rt['fup']]['list'][$rt['fid']]['name'] = $rt['name'];
			if (!empty($rt['threadtypes'])) {
				$forum_list[$rt['fup']]['list'][$rt['fid']]['threadtypes'] = unserialize($rt['threadtypes']);
			}
		} else {
			$forum_arr[$rt['fid']] = $rt['name'];
			$forum_list[$forum_cate[$rt['fup']]]['list'][$rt['fup']]['list'][$rt['fid']]['name'] = $rt['name'];
			if (!empty($rt['threadtypes'])) {
				$forum_list[$forum_cate[$rt['fup']]]['list'][$rt['fup']]['list'][$rt['fid']]['threadtypes'] = unserialize($rt['threadtypes']);
			}
		}
	}
	$forum_str = '<select name="to_fid">';
	$forumtypes_str = $type_info_json = '';
	foreach($forum_list as $key=>$value) {
		if (count($value['list']) > 0) {
			$forum_str .= '<option value="'.$key.'">&gt;&gt; '.$value['name'].'</option>';
			foreach($value['list'] as $k=>$v) {
				$forum_str .= '<option value="'.$k.'"> &nbsp;|- '.$v['name'].'</option>';
				foreach($v['threadtypes']['types'] as $kt=>$vt) {
					$forum_type_list[$k][$kt] = $vt;
					$forumtypes_str .= '<option value="'.$kt.'">'.$vt.'</option>';
				}
				$forumtypes_str .= "</div>";
				if (count($v['list']) > 0) {
					foreach($v['list'] as $kk=>$vv) {
						$forum_str .= '<option value="'.$kk.'"> &nbsp;|-  &nbsp;|-  '.$vv['name'].'</option>';
						foreach($vv['threadtypes']['types'] as $kkt=>$vvt) {
							$forum_type_list[$kk][$kkt] = $vvt;
							$forumtypes_str .= '<option value="'.$kkt.'">'.$vvt.'</option>';
						}
						$forumtypes_str .= "</div>";
					}
				}
			}
		}
	}

}

include template('arpggame_butterfly_data:butterfly_config');